﻿/***************************
* 1.旅行社列表
* 2.旅行社详情
****************************/

/* 变量 */
var travelagent = "elements/travelagent.html";
var travelagentDetail = "elements/travelagentDetail.html";

/* 旅行社列表 */
var contactsCallback_travelagent = myApp.onPageInit('travelagent', function (page) {

    // 过滤 全部美食显示
    $$(".navbar-inner .right img").hide();

    //删除滚动后重新添加滚动
    var flag_InfiniteScroll = true;
    //是否需要滚动加载
    var flag_needScroll = true;
    //滚动事件只执行一次
    var flag_execute = false;

    var index_tab1 = 1;
    var index_tab2 = 1;
    var index_tab3 = 1;
    var current_tabindex = 0;
    var size = 10;
    /* 分类 */
    if(_type == "新县"){
        current_tabindex = 0;
        // 默认加载全部
        LoadData(index_tab1, size);
    }
    else {
        current_tabindex = 1;
        $$("#sub_buttons_row a").eq(0).removeClass("active");
        $$("#sub_buttons_row a").eq(0).hide();
        $$("#sub_buttons_row a").eq(1).addClass("active");
        $$("#travelagent_page #sub_tab1").removeClass("active");
        $$("#travelagent_page #sub_tab2").addClass("active");
        // 默认加载全部
        LoadData(index_tab2, size);
    }

    //加载数据
    function LoadData(pageindex, pagesize) {
        // 检索条件
        var v_search = [];
        var v_order = [];

        var v_longitude = "";
        var v_latitude = "";

        //查询条件
        // 站点
        if (_cityCode != "") {
            var opt = {
                SearchField: "a.CityCode",
                ConditionType: 1,
                AndOR: "and",
                ListSearchValue: [
                    {
                        ItemValue: _cityCode
                    }
                ]
            };
            v_search.push(opt);
        }

        // 排序
        if (current_tabindex == 0) {
            var opt = {
                OrderName: "最新",
                OrderField: "ModifyTime",
                OrderType: "desc"
            };
            v_order.push(opt);
        }
        else if (current_tabindex == 1) {
            var opt = {
                OrderField: "TopPX desc,PX desc,Sort desc",
                OrderType: ""
            };
            v_order.push(opt);
        }
        else if (current_tabindex == 2) {
            var opt = {
                OrderField: "likeCount",
                OrderType: "desc"
            };
            v_order.push(opt);
        }

        if(_isOpenPosition){
            v_longitude = iss_localStorage.get("yuqing_longitude");
            v_latitude = iss_localStorage.get("yuqing_latitude");
        }
        else{ 
            v_longitude = iss_dictionaryBaseData.longitude;
            v_latitude = iss_dictionaryBaseData.latitude;
        }

        var _content = JSON.stringify({
            "type": 0,
            "conditionParams": {
                "Page": {
                    "PageSize": pagesize,
                    "PageIndex": pageindex
                },
                "Search": v_search,
                "Order": v_order
            },
            lng: v_longitude,
            lat: v_latitude,
        });

        $$.ajax({
            url: ISS_Constants.Url.defaulturl + ISS_Constants.serviceName.TRAVELAGENT,
            data: 'content=' + _content,
            crossDomain: true,
            dataType: "jsonp",
            success: function (data, status) {
                data = JSON.parse(data);
                if (data.status == "0" && data.results.length > 0) {
                    // 格式化数据
                    for (var i = 0; i < data.results.length; i++) {
                        if (data.results[i].getImage && data.results[i].getImage.smallImageFrontUrl && (
                            data.results[i].getImage.smallImageFrontUrl == null || data.results[i].getImage.smallImageFrontUrl == "")) {
                            data.results[i].getImage.smallImageFrontUrl = NoPicture();
                        };
                        if(data.results[i].mobileOutUrl && data.results[i].mobileOutUrl != null &&
                           data.results[i].mobileOutUrl != ""){
                           data.results[i].mobileOutUrl = encodeURIComponent(data.results[i].mobileOutUrl);
                        };
                        data.results[i].distance = ISS.utils.toFixed(data.results[i].distance, 2);
                    }

                    var searchTemplate = $$('#template-travelagent-tab').html();
                    if (searchTemplate) {
                        var compiledTemplate = Template7.compile(searchTemplate);
                        var html = compiledTemplate(data);
                        if (current_tabindex == 0) $$("#travelagent_page #sub_tab1 .content-block ul").append(html);
                        else if (current_tabindex == 1) $$("#travelagent_page #sub_tab2 .content-block ul").append(html);
                        else if (current_tabindex == 2) $$("#travelagent_page #sub_tab3 .content-block ul").append(html);
                        flag_InfiniteScroll = flag_needScroll = true;
                    }

                    //tab1综合,无限滚动只加载一次
                    if (!flag_execute) {
                        //只执行一次
                        flag_execute = true;

                        // 加载flag
                        var loading = false;
                        $$('.infinite-scroll').on('infinite', function () {
                            //综合 价格 距离 切换
                            if (!flag_needScroll) {
                                flag_needScroll = true;
                                return;
                            }

                            //查询时,删除滚动后重新绑定pageindex
                            if (!flag_InfiniteScroll) {
                                flag_InfiniteScroll = true;
                                return;
                            }

                            if (true) {
                                // 如果正在加载，则退出
                                if (loading) return;
                                // 设置flag
                                loading = true;
                                // 模拟1s的加载过程
                                setTimeout(function () {
                                    // 重置加载flag
                                    loading = false;
                                    if (current_tabindex == 0) {
                                        index_tab1++;
                                        LoadData(index_tab1, pagesize);
                                    }
                                    else if (current_tabindex == 1) {
                                        index_tab2++;
                                        LoadData(index_tab2, pagesize);
                                    }
                                    else if (current_tabindex == 2) {
                                        index_tab3++;
                                        LoadData(index_tab3, pagesize);
                                    }
                                }, 100);
                            }
                        });
                    }
                    if (current_tabindex == 0 && $$("#travelagent_page #sub_tab1 .content-block ul li").length >= data.total) {
                        // 删除加载提示符
                        $$('.infinite-scroll-preloader1').remove();
                        // 加载完毕，则注销无限加载事件，以防不必要的加载
                        myApp.detachInfiniteScroll($$('.infinite-scroll'));
                    }
                    else if (current_tabindex == 1 && $$("#travelagent_page #sub_tab2 .content-block ul li").length >= data.total) {
                        // 删除加载提示符
                        $$('.infinite-scroll-preloader2').remove();
                        // 加载完毕，则注销无限加载事件，以防不必要的加载
                        myApp.detachInfiniteScroll($$('.infinite-scroll'));
                    }
                    else if (current_tabindex == 2 && $$("#travelagent_page #sub_tab3 .content-block ul li").length >= data.total) {
                        // 删除加载提示符
                        $$('.infinite-scroll-preloader3').remove();
                        // 加载完毕，则注销无限加载事件，以防不必要的加载
                        myApp.detachInfiniteScroll($$('.infinite-scroll'));
                    }
                }
                else {
                    // 删除加载提示符
                    myApp.detachInfiniteScroll($$('.infinite-scroll'));
                    if (current_tabindex == 0) {
                        $$('.infinite-scroll-preloader1').remove();
                        // 删除多余的提示
                        $$("#travelagent_page #sub_tab1 .content-block .noAnswer").remove();
                        if (pageindex == 1) {
                            $$("#travelagent_page #sub_tab1 .content-block").html(ISS_Element.DIV.noAnswer);
                            $$("#travelagent_page #sub_tab1 .noAnswer").on("click", function () {
                                index_tab1=1;
                                LoadData(index_tab1, current_tabindex);
                            });
                        }
                    }
                    else if (current_tabindex == 1) {
                        $$('.infinite-scroll-preloader2').remove();
                        // 删除多余的提示
                        $$("#travelagent_page #sub_tab2 .content-block .noAnswer").remove();
                        if (pageindex == 1) {
                            $$("#travelagent_page #sub_tab2 .content-block").html(ISS_Element.DIV.noAnswer);
                            $$("#travelagent_page #sub_tab2 .noAnswer").on("click", function () {
                                index_tab2=1;
                                LoadData(index_tab2, current_tabindex);
                            });
                        }
                    }
                    else if (current_tabindex == 2) {
                        $$('.infinite-scroll-preloader3').remove();
                        // 删除多余的提示
                        $$("#travelagent_page #sub_tab3 .content-block .noAnswer").remove();
                        if (pageindex == 1) {
                            $$("#travelagent_page #sub_tab3 .content-block").html(ISS_Element.DIV.noAnswer);
                            $$("#travelagent_page #sub_tab3 .noAnswer").on("click", function () {
                                index_tab3=1;
                                LoadData(index_tab3, current_tabindex);
                            });
                        }
                    }
                }
            },
            error: function (xhr, status) { }
        });
    }

    //切换
    $$(".subnavbar").on("click", "a", function () {
        $$('.infinite-scroll').scrollTop(0);
        current_tabindex = $$(this).index();
        myApp.attachInfiniteScroll($$('.infinite-scroll'));
        $$(".noAnswer").remove();

        flag_InfiniteScroll = true;
        flag_needScroll = false;
        if ($$(this).index() == 0 && $$("#travelagent_page #sub_tab1 .content-block ul").children("li").length == 0) {
            LoadData(index_tab1, size);
        }
        else if ($$(this).index() == 1 && $$("#travelagent_page #sub_tab2 .content-block ul").children("li").length == 0) {
            LoadData(index_tab2, size);
        }
        else if ($$(this).index() == 2 && $$("#travelagent_page #sub_tab3 .content-block ul").children("li").length == 0) {
            LoadData(index_tab3, size);
        }
    });
});

/* 旅行社详情  */
var contactsCallback_travelagentDetail = myApp.onPageInit('travelagentDetail', function (page) {
    //获取页面参数
    var scode = ISS.utils.getParameter("scode");
    if (page != undefined) {
        scode = page.query.scode;
    }
    var _content = JSON.stringify({
        type: 1,
        Code: scode
    })
    $$.ajax({
        url: ISS_Constants.Url.defaulturl + ISS_Constants.serviceName.TRAVELAGENT,
        data: { "content": _content },
        crossDomain: true,
        method: "POST",
        dataType: "jsonp",
        success: function (data, status) {
            data = JSON.parse(data);
            if (data && data.status == "0") {
                var searchTemplate = $$('#template-travelagent-detail').html();
                var compiledSearchTemplate = Template7.compile(searchTemplate);
                var html = compiledSearchTemplate(data.results);
                $$("#travelagent_detail_page .page-content").html(html);
            }

            var mySwiper = new Swiper('.swiper-container', {
                pagination: '.swiper-pagination',
                autoplay: 5000,
                speed: 300,
            });
        },
        error: function (xhr, status) {
            console.log("ajax error!");
        }
    });
});

/* 初始化  */
function initData() {
    var href = window.location.href;
    if (href.indexOf(travelagent) > -1) {
        contactsCallback_travelagent.trigger();
    }
    else if (href.indexOf(travelagentDetail) > -1) {
        contactsCallback_travelagentDetail.trigger();
    }
}

initData();

